<?php
/*
# ------------------------------------------------------------------------
# JA Job Board Package
# ------------------------------------------------------------------------
# Copyright (C) 2004-2010 JoomlArt.com. All Rights Reserved.
# @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
# Author: JoomlArt.com
# Websites: http://www.joomlart.com - http://www.joomlancers.com.
# ------------------------------------------------------------------------
*/

/**
* This controller is used for JAEmail feature of the component
*/
class JAJobBoardControllerJASalary extends JAJobBoardController {
/**
     * Constructor
     */
    function __construct( $location = array() )
    {
    	
        parent::__construct( $location );      
        // Register Extra tasks
        $this->registerTask( 'add',     'edit' );
        $this->registerTask( 'apply',   'save' );        
		$this->registerTask( 'unpublish', 	'publish');
    }
    
    /**
    * Display current jasalary of the component to administrator
    * 
    */
    function display(){
    	
    	switch($this->getTask())
		{
			case 'add'     :
			{
				JRequest::setVar( 'hidemainmenu', 1 );
				JRequest::setVar( 'edit', false );
				JRequest::setVar( 'layout', 'form' );
				// Checkout the jaconfig
				$model = $this->getModel('jasalary');
				$model->checkout();
			} break;
			case 'edit':
			{
				JRequest::setVar( 'hidemainmenu', 1 );
				JRequest::setVar( 'edit', true );
				JRequest::setVar( 'layout', 'form' );
				// Checkout the jaconfig
				$model = $this->getModel('jasalary');
				$model->checkout();
			} break;
		}
		
		parent::display();
    }
    
    /**
    * Cancel current operation
    * 
    */
    function cancel(){
    	global $option;
        $this->setRedirect("index.php?option=$option&view=jasalary");
    }   
    /**
    * Remove a jasalary row
    * 
    */   
    function remove(){
    	global $option;
 
    	// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );
        
        $model =& $this->getModel('jasalary');
        if(($n = $model->remove()) < 0){
             JError::raiseWarning( 500, $row->getError() );
		}
		
		$this->setMessage(JText::sprintf('Items removed', $n));
		$this->setRedirect("index.php?option=$option&view=jasalary");
	}
	
	
	/**
	* Save categories record
	*/
	function save(){
		global $option;
		// Check for request forgeries
		JRequest::checkToken() or jexit( 'Invalid Token' );

		$cache = & JFactory::getCache($option);
		$cache->clean();

		$model	=& $this->getModel('jasalary');
		$post	= JRequest::get('post');
		
		// allow name only to contain html
		$post['name'] = JRequest::getVar( 'name', '', 'post', 'string', JREQUEST_ALLOWHTML );
		
		$paramsField = JRequest::getVar( 'params', null, 'post', 'array' );				
		if ($paramsField)
		{	
			$params = new JParameter('');
			$data = array();
			foreach ($paramsField as $k => $v) {
				$params->set($k, $v);					
			}					
			$post['params'] = $params->toString();
		}
			
		$model->setState( 'request', $post );
		
		if ($id = $model->store()) {
			$msg = JText::_( 'The Location item Saved' );
		} else {
			$msg = JText::_( 'Error Saving Location item' );
		}
		
		switch ( $this->_task ) {
			case 'apply':
				$this->setRedirect( "index.php?option=$option&view=jasalary&task=edit&cid[]=$id" , $msg );
				break;

			case 'save':
			default:
				$this->setRedirect( "index.php?option=$option&view=jasalary", $msg );
				break;
		}
	}	
	
	/**
	* change Is_Published status
	*/
	/**
	 * publish or unpublish list item 
	 * @return void
	 **/
	function publish(){
		global  $option;
		if($this->getTask()=='unpublish'){
			$publish = 0;
		}
		else{
			$publish = 1;
		}
		
		$model = $this->getModel('jasalary');
				
		if (!$model->dopublish($publish)) {
			echo "<script> alert('".$model->getError(true)."'); window.history.go(-1); </script>\n";
			exit();
		}
		$cache = & JFactory::getCache($option);
		$cache->clean();	
		$this->setRedirect("index.php?option=$option&view=jasalary", "Update successfully !");
	}		
    
    /**
    * Move down the order of field list item
    * 
    */
    function orderdown(){
    	global $option;
        // Check for request forgeries
        JRequest::checkToken() or jexit( 'Invalid Token' );
        
        $cid    = JRequest::getVar( 'cid', array(), 'post', 'array' );
        JArrayHelper::toInteger($cid);
        
        $task = JRequest::getVar('task', 'orderdown');

        if (isset($cid[0]) && $cid[0]) {
            $id = $cid[0];
        } else {
            $this->setRedirect( "index.php?option=$option&view=jasalary", JText::_('No Items Selected') );
            return false;
        }

        $model =& $this->getModel( 'jasalary' );
        $movement = $task == 'orderdown' ? 1 : -1;

        if($model->orderItem($id, $movement)){
            $msg = $movement > 0 ? JText::_( 'Item Moved Down' ) : JText::_( 'Item Moved Up' );
        }
        
        $this->setRedirect( "index.php?option=$option&view=jasalary", $msg );
    }
    
    /**
    * Save the item(s) order
    */
    function saveorder()
    {
    	global $option;
        // Check for request forgeries
        JRequest::checkToken() or jexit( 'Invalid Token' );
        
        $cid    = JRequest::getVar( 'cid', array(), 'post', 'array' );
        JArrayHelper::toInteger($cid);

        $model =& $this->getModel( 'jasalary' );
        if ($model->setOrderchu($cid)) {
            $msg = JText::_( 'New ordering saved' );
        } else {
            $msg = $model->getError();
        }
        $this->setRedirect(  "index.php?option=$option&view=jasalary", $msg );
    }
}